<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
//引入Auth门面
use Auth;

class PublicController extends Controller
{
    //展示登录页面
    public function login() {
    	return view('admin.public.login');
    }
    //验证用户
    public function checkLogin(Request $request) {
    	//填写验证校准
    	$this -> validate($request,[
    			//校验用户
    			'username' => 'required|min:2|max:20',
    			//校验密码
    			'password' => 'required|min:6|max:30',
    			//校验验证码
    			'captcha' => 'required|size:5|captcha'
    		]);

    	//获取用户和密码
    	$data = $request -> only('username','password');
    	//校验当前用户是否停用
    	$data['status'] = 2;
    	//开始认证用户信息
    	$result = Auth::guard('admin') -> attempt($data,$request -> get('online'));
    	// dd($result);
    	//判断用户是否登录成功
    	if($result) {
    		//登录成功跳转到后台页面
    		return redirect('/admin/index/index');
    	} else {
    		return redirect('/admin/public/login') -> withErrors([
    				'loginError' => '用户名和密码错误！'
    			]);
    	}
    }

    //用户退出的方法
    public function logout() {
    	//清空session信息
    	Auth::guard('admin') -> logout();
    	//跳转到登录页面
    	return redirect('/admin/public/login');
    }
}
